﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Net.Configuration;
using ClassLibrary1;
using System.Net.Mail;
using System.IO;



namespace Bilyteam_Website
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            BindProvices();
            BindSend();
        }

        private void BindSend()
        {
            binContacts bin = new binContacts();
            DataTable list = bin.getTeams();
            ddlBophan.DataSource = list;
            ddlBophan.DataTextField = "TeamName";
            ddlBophan.DataValueField = "TeamsID";
            ddlBophan.DataBind();
        }

        private void BindProvices()
        {

            binContacts bin = new binContacts();
            DataTable list = bin.getProvices();
            ddlTThanh.DataSource = list;
            ddlTThanh.DataTextField = "ProviceName";
            ddlTThanh.DataValueField = "ProviceID";

            ddlTThanh.DataBind();
        }
        public static String FormAddress
        {
            get
            {
                SmtpSection cfg = (SmtpSection)ConfigurationManager.GetSection
                    ("system.net/mailSettings/smtp");
                return cfg.Network.UserName;
            }
        }
        public string SendMail(string subject, string body, string to, bool isHtml, bool isSSL)
        {
            try
            {
                using (MailMessage mail = new MailMessage())
                {
                    mail.From = new MailAddress(FormAddress, "designlife.VN");
                    mail.To.Add(to);
                    mail.Subject = subject;
                    mail.Body = body;
                    mail.IsBodyHtml = isHtml;
                    SmtpClient client = new SmtpClient();
                    client.EnableSsl = isSSL;
                    client.Send(mail);
                }

            }

            catch (SmtpException ex)
            {
                return ex.Message;

            }

            return lblLoi.Text = "Chúng tôi đã nhận được yên cầu.xin kiểm tra lại mail";
        }
        protected void btnGui_Click(object sender, EventArgs e)
        {
            try
            {
                if (recaptcha.IsValid == true)
                {
                    string IDProvices = ddlTThanh.SelectedItem.Value;
                    int IDTeam = int.Parse(ddlBophan.SelectedItem.Value);
                    string FullName = txtHvten.Text;
                    int Gender = int.Parse(ddlGender.SelectedItem.Value);
                    string CompanyName = txtCty.Text;
                    string Position = txtVitri.Text;
                    string Address = txtDaichi.Text;
                    string Telephone = txtPhone.Text;
                    string Email = txtEmail.Text;
                    string Website = txtWeb.Text;
                    string MainContent = txtNoiDung.Text;


                    conn.ConnectionString = ConfigurationManager.ConnectionStrings["PROJECT"].ConnectionString;
                    conn.Open();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into Contacts values (@IDTeam, @IDProvices, @FullName, @Gender, @CompanyName, @Position, @Address, @Telephone, @Email, @Website, @MainContent, @Reader)";
                    cmd.CommandType = CommandType.Text;

                    cmd.Parameters.Add("@IDTeam", SqlDbType.Int);
                    cmd.Parameters.Add("@IDProvices", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@FullName", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@Gender", SqlDbType.Bit);
                    cmd.Parameters.Add("@CompanyName", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@Position", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@Address", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@Telephone", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@Email", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@Website", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@MainContent", SqlDbType.NVarChar);
                    cmd.Parameters.Add("@Reader", SqlDbType.Bit);

                    cmd.Parameters[0].Value = IDTeam;
                    cmd.Parameters[1].Value = IDProvices;
                    cmd.Parameters[2].Value = FullName;
                    cmd.Parameters[3].Value = Gender;
                    cmd.Parameters[4].Value = CompanyName;
                    cmd.Parameters[5].Value = Position;
                    cmd.Parameters[6].Value = Address;
                    cmd.Parameters[7].Value = Telephone;
                    cmd.Parameters[8].Value = Email;
                    cmd.Parameters[9].Value = Website;
                    cmd.Parameters[10].Value = MainContent;
                    cmd.Parameters[11].Value = 0;

                    StreamReader sr = new StreamReader(Server.MapPath("template/Contact.htm"));
                    sr = File.OpenText(Server.MapPath("template/Contact.htm"));
                    string content = sr.ReadToEnd();

                    content = content.Replace("[Name]", txtHvten.Text.Trim());
                    content = content.Replace("[Email]", txtEmail.Text);
                    content = content.Replace("[Content]", txtNoiDung.Text);
                    content = content.Replace("[DateTime]", DateTime.Now.ToShortDateString());
                    try
                    {
                        lblLoi.Text = (SendMail("Liên hệ khách hàng", content, txtEmail.Text.Trim(), true, false));

                    }
                    catch (Exception ex)
                    {
                        lblLoi.Text = "";
                    }

                    int process = cmd.ExecuteNonQuery();
                    conn.Close();
                    if (process > 0)
                    {
                        lblErro.Text = "Gửi Thành Công";
                        txtCty.Text = "";
                        txtDaichi.Text = "";
                        txtEmail.Text = "";
                        txtHvten.Text = "";
                        txtNoiDung.Text = "";
                        txtPhone.Text = "";
                        txtVitri.Text = "";
                        txtWeb.Text = "";
                    }

                    else
                    {
                        lblErro.Text = "Gửi Thất Bại";

                    }
                    lblCapcha.Text = "";
                }
                else
                {
                    lblCapcha.Text = "mã xác thực không đúng";
                    lblErro.Text = "Gửi Thất Bại";
                    lblLoi.Text = "";
                }
            }
            catch (Exception ex)
            {
                lblErro.Text = "Gửi Thất Bại";

            }
        }
       
       
    }
}